<?php Yii::app()->clientScript->beginScript('custom-script'); ?>
    <script type="text/javascript">
        var optionList = $('[name="id_matter_task[]"]').bootstrapDualListbox({
            bootstrap2compatible: true,
            preserveselectiononmove : false,            // 'all' / 'moved' / false
            moveonselect            : true,             // true/false (forced true on androids, see the comment later)
            initialfilterfrom       : '',               // string, filter selectables list on init
            initialfilterto         : '',               // string, filter selected list on init
            helperselectnamepostfix     : '_helper',    // 'string_of_postfix' / false
            infotext                : false, // 'Showing all {0}',// text when all options are visible / false for no info text
            infotextfiltered        : '<span class="label label-warning">Filtered</span> {0} from {1}',// when not all of the options are visible due to the filter
            infotextempty           : 'Empty list',      // when there are no options present in the list
            selectorminimalheight   : 100,
            showfilterinputs        : false, //true,
            filterplaceholder       : 'Filter',
            filtertextclear         : 'show all',
            nonselectedlistlabel    : '<?php echo Yii::t('app', 'Task ID available') ?>',            // 'string', false
            selectedlistlabel       : '<?php echo Yii::t('app', 'Task ID in this track') ?>'             // 'string', false
        });

        $(document).ready(function() {
            var baseUrl = '<?php echo Yii::app()->getBaseUrl(); ?>';
            $('#Tracking_id_client').change(function(){
                var idClient = $("#Tracking_id_client option:selected").val();
                $.ajax({
                    type: "POST",
                    url: "<?php echo CController::createUrl('tracking/filterTaskByClient') ?>",
                    data: { id_client : idClient },
                    dataType: "html"
                }).done(function( data ) {
                    $('#id_matter_task').html(data);
                    optionList.trigger('bootstrapduallistbox.refresh', true);
                });
            });
            <?php
            /**
             * comment by hoanx(signature not using transporter)
             *
            $('#Tracking_id_tpdetail').change(function(){
                var idTpdetail = $("#Tracking_id_tpdetail option:selected").val();
                if(idTpdetail){
                    $.ajax({
                        type: "POST",
                        url: "<?php //echo CController::createUrl('tracking/fillSignatureByTransporter') ?>",
                        data: { id_transporter : idTpdetail },
                        dataType: "html"
                    }).done(function( data ) {
                            $('#image_signature').html(data);
                        });
                }
            });
             */
             ?>
            //register draw
            $('.signature-pad').signaturePad({
                drawOnly : true,
                lineWidth : 0,
                validateFields : false,
                errorMessageDraw : '<?php echo Yii::t('app', 'Please add signarute!') ?>'
            });

            //submit form
            $('#tracking_add_form').submit(function(){
                var signature = $('#data_signature').val();
            });

            <?php
            /**
             * comment generate signature

            $('#generate_ignature').click(function(){
                var signature = $('#data_signature').val();
                var transporter = $("#Tracking_id_tpdetail option:selected").val();
                $.ajax({
                    type: "POST",
                    url: "<?php //echo CController::createUrl('tracking/generateSignature') ?>",
                    dataType:'json',
                    data: { data :  signature, id_transporter : transporter}
                }).done(function( data ) {
                    if(data.hasError){
                        $( ".signature-pad" ).prepend(data.ErrorMessage);
                    }else{
                        $('#image_signature').html(data.result);
                    }
                    return false;
                });
            });
             */
             ?>
        });
    </script>
<?php Yii::app()->clientScript->endScript();?>

<?php
$flashMessages = Yii::app()->user->getFlashes();
if ($flashMessages) {
    foreach($flashMessages as $key => $message) {
        echo '<div class="alert alert-' . $key . ' fade in"><button type="button" class="close" data-dismiss="alert">×</button>' . $message . "</div>\n";
    }
}
?>
<section class="add-record">
    <?php $form = $this->beginWidget('bootstrap.widgets.TbActiveForm', array(
        'id'=>'tracking_add_form',
        'type'=>'horizontal',
    ));?>

    <fieldset>
        <legend>
            <?php echo Yii::t('app', 'Add new record') ?>
            <a class="btn-back pull-right" href="<?php echo $this->createUrl('tracking/index')?>">← <?php echo Yii::t('app', 'Back to List') ?></a>
        </legend>
        <?php

        echo $form->dropDownListRow($model, 'id_client', $listClient,
            array(
                'empty' => Yii::t('app', 'Please select client'),
            )
        )
        ?>
        <?php echo $form->dropDownListRow($model, 'id_tpdetail', $listTrensporter, array('empty'=>Yii::t('app', 'Please select transported'))) ?>

        <div class="control-group ">
            <label for="Task_delivery_date" class="control-label required">
                <?php echo Yii::t('app', 'Shipping Date') ?> <span class="required">*</span>
            </label>
            <div class="controls">
                <?php
                $this->widget('zii.widgets.jui.CJuiDatePicker', array(
                    'model' => $model,
                    'attribute' => 'ship_date',
                    'options' => array(
                        'dateFormat' => 'yy-mm-dd',
                    ),
                    'htmlOptions' => array(
                        'maxlength' => 8,
                        'readonly' => 'readonly',
                    ),
                ));
                ?>
            </div>
        </div>

        <div class="control-group ">
            <div class="controls">
                <select multiple="multiple" size="10" name="id_matter_task[]" id="id_matter_task">
                    <?php if($model->id_client): ?>
                        <?php $listTaskByClient = Task::_getTaskByClient($model->id_client); ?>
                        <?php if($listTaskByClient): ?>
                            <?php foreach($listTaskByClient as $task): ?>
                                <option value="<?php echo $task->id_task; ?>"><?php echo $task->id_by_matter; ?></option>
                            <?php endforeach; ?>
                        <?php endif ?>
                    <?php endif ?>
                </select>
            </div>
        </div>

        <div class="control-group">
            <div class="controls signature-pad">
                <!--<div>
                    <button name="clearButton" class="btn btn-warning clearButton"><i class="icon-remove icon-white"></i> <?php /*echo Yii::t('app', 'Clear') */?></button>
                    &nbsp;&nbsp;
                    <button id="generate_ignature" name="generate_ignature" class="btn btn-success" type="button">
                        <i class="icon-ok icon-white"></i> <?php /*echo Yii::t('app', 'Add / modify Signature') */?>
                    </button>
                </div>
                <br>-->

                <div class="canvas-signature">
                    <div class="pull-right" id="image_signature">
                        <?php if($model->id_tpdetail): ?>
                            <?php echo CHtml::image(Yii::app()->getBaseUrl().'/signature/'.$model->id_tpdetail.'.png'); ?>
                        <?php endif ?>
                    </div>
                    <canvas class="canvas-pad" width="<?php echo Constant::IMG_WIDTH_SIGNATURE ?>" height="<?php echo Constant::IMG_HEIGHT_SIGNATURE ?>"></canvas>
                    <input type="hidden" name="data_signature" id="data_signature" class="output" value="">
                </div>

            </div>
        </div>

    </fieldset>
    <div class="form-actions">
        <?php
        $this->widget('bootstrap.widgets.TbButton', array(
            'buttonType' => 'submit',
            'type' => 'primary',
            'label' => Yii::t('app', 'Submit'),
            'icon' => 'plus-sign white'
        ));
        ?>
    </div>
    <?php $this->endWidget();?>
</section>
